<template>
<el-container class="home-container">
    <!-- 使用了el header 表达式 -->
  <el-header>
      <div>
          <img src="../assets/favicon.jpg">
          <span>芙兰朵露的小超市</span>
      </div>
  <el-button type="info" @click="logout">退出</el-button>
  </el-header>
  <!-- 缩进按钮 -->
  <!-- 页面主体 -->
  <el-container>
      <!-- 侧边栏 -->
    <el-aside :width="isCollapsr ?'64px':'200px'">  <!--三目运算符 判断侧边栏是否折叠-->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区 -->
        <el-menu
        background-color="#333744"
        text-color="#fff"
        active-text-color="#409eff"
        unique-opened
        :collapse="isCollapsr"
        :collapse-transition="false"
        router
        :default-active="activePath"
        >
        <!-- 一级菜单 -->
            <el-submenu :index='item.id+""' v-for='item in munulist' :key='item.id'>
                <!-- 一级菜单的模板 -->
                <template slot="title">
                        <!-- 图标 -->
                <i :class="iconObj[item.id]"></i>
                <!-- 文本 -->
                <span>{{ item.authName }}</span>
                </template>
                <!-- 二级 -->
                <el-menu-item :index="'/'+child.path" v-for='child in item.children' :key='child.id' @click="saveNavState('/'+child.path)">
                    <!-- 二级菜单的模板 -->
                    <template slot="title">
                    <!-- 图标 -->
                    <i class="el-icon-caret-right"></i>
                    <!-- 文本 -->
                    <span>{{ child.authName}}</span>
                    </template>
                </el-menu-item>
            </el-submenu>
        </el-menu>
    </el-aside>
    <!-- 侧边主体 -->
    <el-main>
        <!-- 子路由 -->
        <router-view></router-view>
    </el-main>
  </el-container>
</el-container>
</template>

<script>
export default {
  data () {
    return {
      // 左侧菜单数据存放
      munulist: [],
      iconObj: {
        125: 'el-icon-menu',
        103: 'el-icon-circle-plus-outline',
        101: 'el-icon-goods',
        102: 'el-icon-document',
        145: 'el-icon-view'
      },
      isCollapsr: false,
      // 被激活的地址
      activePath: ''
    }
  },
  // 页面创建之前调用自动调用这个而函数
  created () {
    //  调用获得菜单的函数，来获取菜单
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.munulist = res.data
      console.log(res)
    },
    toggleCollapse () {
      this.isCollapsr = !this.isCollapsr
    },
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.el-header{
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding: 0;
    // 元素上下居中
    align-items: center;
    color:#fff;
    font-size: 20px;
    > div {
        display: flex;
        align-items: center;
        img {
            height: 80px;
            border-radius: 50%;
        }
        span {
            margin-left: 15px;
        }
    }
}
.el-aside{
    background-color: #333744;
    .el-menu{
        border-right: 0;
         transition: all 0.5s
    }
}
.el-main{
    background-color: #eaedf1;
}
.home-container{
    height: 100%;
}
.iconfont {
    margin-right: 10px;
}
.toggle-button{
    background-color: #4A5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
}
</style>
